public class PdfToImageConverter implements Converter {
    @Override
    public boolean support(FileFormat sourceFormat, FileFormat targetFormat) {
        return sourceFormat == FileFormat.PDF && 
               (targetFormat == FileFormat.PNG || targetFormat == FileFormat.JPG);
    }
    
    @Override
    public void convert(File source, File target, ConversionConfig config) throws ConversionException {
        try {
            if (!source.exists()) {
                throw new FileOperationException("源文件不存在: " + source.getPath());
            }
            
            PDDocument document = PDDocument.load(source);
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            
            int dpi = Integer.parseInt(config.getCustomParams().getOrDefault("dpi", "300"));
            float quality = config.getQuality() / 100.0f;
            
            BufferedImage image = pdfRenderer.renderImageWithDPI(0, dpi);
            
            String formatName = target.getName().substring(target.getName().lastIndexOf('.') + 1);
            
            ImageWriter writer = ImageIO.getImageWritersByFormatName(formatName).next();
            ImageWriteParam params = writer.getDefaultWriteParam();
            
            if (params.canWriteCompressed()) {
                params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                params.setCompressionQuality(quality);
            }
            
            ImageOutputStream output = ImageIO.createImageOutputStream(target);
            writer.setOutput(output);
            writer.write(null, new IIOImage(image, null, null), params);
            
            document.close();
            output.close();
            writer.dispose();
            
        } catch (IOException e) {
            throw new FileOperationException("PDF转换图片过程中发生错误", e);
        } catch (NumberFormatException e) {
            throw new InvalidConfigurationException("DPI配置参数无效");
        }
    }
} 